package com.iris.live.services.interceptors;

import com.google.common.base.Strings;
import com.iris.live.services.services.AccountService;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * Created by xin on 2016/7/31.
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {
    private static Logger logger = LoggerFactory.getLogger(SessionInterceptor.class);

    public static final String AttributeNameLicense = SessionInterceptor.class.getName() + ".license";
    public static final String AttributeNameUsername = SessionInterceptor.class.getName() + ".username";
    public static final String AttributeNameIsLoggedIn = SessionInterceptor.class.getName() + ".isLoggedIn";
    public static final String AttributeNameCurrentUser = SessionInterceptor.class.getName() + ".user";
    public static final String AttributeNameLanguage = SessionInterceptor.class.getName() + ".language";

    private static final String HeaderLicense = "live-license";
    private static final String HeaderUsername = "live-username";
    private static final String HeaderVersion = "live-version";
    private static final String HeaderPatch = "live-patch";
    private static final String HeaderLanguage = "live-language";

    @Autowired
    private AccountService accountService;

    public static boolean isLoggedIn(HttpServletRequest request) {
        return Boolean.TRUE.equals(request.getAttribute(AttributeNameIsLoggedIn));
    }

    public static String getLicense(HttpServletRequest request) {
        return (String) request.getAttribute(AttributeNameLicense);
    }

    public static String getUsername(HttpServletRequest request) {
        return (String) request.getAttribute(AttributeNameUsername);
    }

    public static Object getCurrentUser(HttpServletRequest request) {
        return request.getAttribute(AttributeNameCurrentUser);
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String license = request.getHeader(HeaderLicense);
        String username = request.getHeader(HeaderUsername);
        String version = request.getHeader(HeaderVersion);
        String patch = request.getHeader(HeaderPatch);
        String language = request.getHeader(HeaderLanguage);
        if(language == null || language.trim().equals("")){
            language ="CN";
        }

        ThreadContext.put("UUID", UUID.randomUUID().toString().replaceAll("-", ""));
        ThreadContext.put("LIVE_LANGUAGE", language);
        ThreadContext.put("LIVE_USERNAME", username);
        logger.warn("live-license={}， live-username={}, live-version={}， live-patch={}, live_language={}, url={}", license,username, version, patch, language, request.getRequestURL());

        request.setAttribute(AttributeNameLicense, license);
        request.setAttribute(AttributeNameUsername, username);
        request.setAttribute(AttributeNameIsLoggedIn,!Strings.isNullOrEmpty(license) && !Strings.isNullOrEmpty(username));
        request.setAttribute(AttributeNameLanguage, language);

        return true;
    }



    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadContext.clearAll();
        super.afterCompletion(request, response, handler, ex);
    }
}
